/*
WaitGroup 相当于Java里面的CountDownLatch
关键方法 Add() Wait() Done()
*/
package main

import (
	"fmt"
	"strings"
	"sync"
	"time"
)

func main() {
	var wg sync.WaitGroup
	fmt.Println("wg:", wg)
	wg.Add(3)

	go printNum(&wg, 1)
	go printNum(&wg, 2)
	go printNum(&wg, 3)
	wg.Wait()
	defer fmt.Println("end")
}

func printNum(wg *sync.WaitGroup, num int) {
	for i := 1; i <= 3; i++ {
		pre := strings.Repeat("\t", num-1)
		fmt.Println(pre, num, "_", i)
		time.Sleep(time.Second)
	}
	wg.Done()
}
